package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.ChRuKuService;
import cn.gson.boot.model.service.FaHuoService;
import cn.gson.boot.model.service.HeTongService;
import cn.gson.boot.model.service.TuiHuoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tuihuo")
public class TuiHuoController {
    @Autowired
    HeTongService heTongService;
    @Autowired
    TuiHuoService tuiHuoService;

    //根据选择的订单查询订单详情(退货)
    @RequestMapping("/tuihuoCkDdxq")
    @ResponseBody
    public List<Dingdanxiangqing> tuihuoCkDdxq(Integer ddid){
        List<Dingdanxiangqing> ddxq = heTongService.ckDingDanXiangQing(ddid);
        //判断此订单详情里面的产品是不是都退了（和退货详情里面的产品数量是否相同）
        List<Tuihuoxiangqing> thxq = tuiHuoService.selectAllTuiHuoXiangQin();
        Dingdan dd = null;
        for (int i=0;i<ddxq.size();i++){
            System.err.println("i:"+i+"||"+ddxq.get(i).getDingdanByDdId());
            for(int j=0;j<thxq.size();j++){
                //根据退货id查看订单对象
                dd = tuiHuoService.selectDingdanByThId(thxq.get(j).getTuihuoByThId().getThId());
                //判断是否是同一个订单
                if(ddxq.get(i).getDingdanByDdId().getDdId() == dd.getDdId()){
                    System.err.println("判断是否是同一个订单");
                    //判断是否是同一个产品
                    if(ddxq.get(i).getProductt().getProducttId() == thxq.get(j).getProductt().getProducttId()){
                        System.err.println("判断是否是同一个产品");
                        //判断产品数量是否相同
                        if(ddxq.get(i).getDdcpNum() == thxq.get(j).getThcpNum()){
                            System.err.println("如果相同就移除");
                            //如果相同就移除
                            ddxq.remove(i);
                        }else {
                            System.err.println("不同就修改数量");
                            //不同就修改数量
                            ddxq.get(i).setDdcpNum(ddxq.get(i).getDdcpNum() - thxq.get(j).getThcpNum());
                        }
                    }
                }
            }
        }
        return ddxq;
    }

    //根据选择的订单查询订单详情（换货）
    @RequestMapping("/tuihuoCkDdxq22")
    @ResponseBody
    public List<Dingdanxiangqing> tuihuoCkDdxq22(Integer ddid){
        List<Dingdanxiangqing> ddxq = heTongService.ckDingDanXiangQing(ddid);
        //判断此订单详情里面的产品是不是都退了（和退货详情里面的产品数量是否相同）
        List<Huanhuoxiangqing> thxq = tuiHuoService.selectAllHuanHuoXiangQin();
        Dingdan dd = null;
        for (int i=0;i<ddxq.size();i++){
            System.err.println("i:"+i+"||"+ddxq.get(i).getDingdanByDdId());
            for(int j=0;j<thxq.size();j++){
                //根据换货id查看订单对象
                dd = tuiHuoService.selectDingdanByhhId(thxq.get(j).getHuanhuoByHhId().getHhId());
                //判断是否是同一个订单
                if(ddxq.get(i).getDingdanByDdId().getDdId() == dd.getDdId()){
                    System.err.println("判断是否是同一个订单");
                    //判断是否是同一个产品
                    if(ddxq.get(i).getProductt().getProducttId() == thxq.get(j).getProductt().getProducttId()){
                        System.err.println("判断是否是同一个产品");
                        //判断产品数量是否相同
                        if(ddxq.get(i).getDdcpNum() == thxq.get(j).getHhcpNum()){
                            System.err.println("如果相同就移除");
                            //如果相同就移除
                            ddxq.remove(i);
                        }else {
                            System.err.println("不同就修改数量");
                            //不同就修改数量
                            ddxq.get(i).setDdcpNum(ddxq.get(i).getDdcpNum() - thxq.get(j).getHhcpNum());
                        }
                    }
                }
            }
        }
        return ddxq;
    }
    //新增退货
    @RequestMapping("/addTuiHuo")
    @ResponseBody
    public void addTuiHuo(@RequestBody Tuihuo tuihuo){
        tuiHuoService.addTuiHuo(tuihuo);
    }

    //查询所有退货
    @RequestMapping("/selectAllTuiHuo")
    @ResponseBody
    public Map<String,Object> selectAllTuiHuo(String kName,String ddHao,String time, Integer pageNumm,
                                              Integer pageSizee,HttpSession session){
        Page<Object> objects = PageHelper.startPage(pageNumm, pageSizee);

        String time3 = null;
        if(time != null && time != "" && !time.equals("")){
            StringBuffer time1 = new StringBuffer(time.substring(0,10));
            System.err.println("tim1:"+time1);
            int ti = Integer.parseInt(time1.substring(8))+1;
            System.err.println("ti:"+ti);
            String tistr = toString().valueOf(ti);
            if(toString().valueOf(ti).length() == 1){
                tistr = 0 + toString().valueOf(ti);
            }
            StringBuffer time2 = new StringBuffer(time1.replace(8,time1.length(), tistr));
            System.err.println("tim2:"+time2);
            time3 = time2.toString();
        }

        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("销售经理")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        List<Tuihuo> list = tuiHuoService.selectAllTuiHuo(kName,ddHao,time3,empName);

        Map<String,Object> map = new HashMap();
        map.put("total", objects.getTotal());
        map.put("list", list);
        return map;
    }


    //根据退货id查看退货详情
    @RequestMapping("/ckTuiHuoXiangQin")
    @ResponseBody
    public List<Tuihuoxiangqing> ckTuiHuoXiangQin(Integer thId){
        return tuiHuoService.ckTuiHuoXiangQin(thId);
    }

    //移除退货
    @RequestMapping("/yichuTuiHuo")
    @ResponseBody
    public String yichuTuiHuo(Integer thId){
        return tuiHuoService.yichuTuiHuo(thId);
    }


    /*===换货====================================================================*/
    //新增换货
    @RequestMapping("/addHuanHuo")
    @ResponseBody
    public void addHuanHuo(@RequestBody Huanhuo huanhuo){
        tuiHuoService.addHuanHuo(huanhuo);
    }

    //查看所有换货
    @RequestMapping("/selectAllHuanHuo")
    @ResponseBody
    public Map<String,Object> selectAllHuanHuo(String kName,String ddHao,String time, Integer pageNumm,
                                               Integer pageSizee,HttpSession session){
        Page<Object> objects = PageHelper.startPage(pageNumm, pageSizee);

        String time3 = null;
        if(time != null && time != "" && !time.equals("")){
            StringBuffer time1 = new StringBuffer(time.substring(0,10));
            System.err.println("tim1:"+time1);
            int ti = Integer.parseInt(time1.substring(8))+1;
            System.err.println("ti:"+ti);
            String tistr = toString().valueOf(ti);
            if(toString().valueOf(ti).length() == 1){
                tistr = 0 + toString().valueOf(ti);
            }
            StringBuffer time2 = new StringBuffer(time1.replace(8,time1.length(), tistr));
            System.err.println("tim2:"+time2);
            time3 = time2.toString();
        }

        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("销售经理")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }

        List<Huanhuo> list = tuiHuoService.selectAllHuanHuo(kName,ddHao,time3,empName);

        Map<String,Object> map = new HashMap();
        map.put("total", objects.getTotal());
        map.put("list", list);
        return map;
    }


    //根据换货id查找换货详情
    @RequestMapping("/ckHuanHuoXiangQin")
    @ResponseBody
    public List<Huanhuoxiangqing> ckHuanHuoXiangQin(Integer hhId){
        return tuiHuoService.ckHuanHuoXiangQin(hhId);
    }

    //移除换货
    @RequestMapping("/yichuHuanHuo")
    @ResponseBody
    public String yichuHuanHuo(Integer hhId){

        return tuiHuoService.yichuHuanHuo(hhId);
    }

    //退换货：查看所有(执行中)订单
    @RequestMapping("/tuihuanDDzxz")
    @ResponseBody
    public List<Dingdan> tuihuanDDzxz(Integer kId){
        List<Dingdan> dd = heTongService.tuihuanDDzxz(kId);
        List<Dingdan> dd22 = new ArrayList<>();
        List<Fahuo> fahuos = null;
        for(int i=0;i<dd.size();i++){
            fahuos = selectFaHuoByddIdTHfhcg(dd.get(i).getDdId());
            System.out.println("SizeSize："+fahuos.size());
            if(fahuos.size() == 0){
                System.out.println("我是看的空的");
                dd22.add(dd.get(i));
            }
            System.out.println("发乎的订单："+fahuos);
        }
        System.out.println("dd22dd22dd22dd22："+dd22);
        for(int a=dd.size()-1;a>=0;a--){
            for(int b=dd22.size()-1;b>=0;b--){
                if(dd.get(a)==dd22.get(b)){
                    dd.remove(a);
                    break;
                }
            }
        }
      /* System.err.println("==========================");
       for(int a=0;a<dd.size();a++){
           System.out.println("a:"+dd.get(a).getDdHao());
       }
        System.err.println("==========================");
        for(int b=0;b<dd22.size();b++){
            System.out.println("b:"+dd22.get(b).getDdHao());
        }
        System.err.println("==========================");
        dd.removeAll(dd22);
        for(int c=0;c<dd.size();c++){
            System.out.println("c:"+dd.get(c).getDdHao());
        }*/
        return dd;
    }

    @Autowired
    FaHuoService faHuoService;
    //{退货}据ddId查到此订单的所有发货状态为‘发货成功’的发货单（退货）(换货){退换货都只能一次}
    @RequestMapping("/selectFaHuoByddIdTHfhcg")
    @ResponseBody
    public List<Fahuo> selectFaHuoByddIdTHfhcg(Integer ddId){
        //根据订单id找到此订单的所有发货单
        List<Fahuo> fh = faHuoService.selectFaHuoByddIdTHfhcg(ddId);
        List<Fahuo> fhok = new ArrayList<>();
        //查看所有的退货单
        List<Tuihuo> thlist = tuiHuoService.selectAllTuiHuo(null,null,null,null);
        System.err.println("thlistSize:"+thlist.size());
        if(thlist.size() != 0){
            for(int i=0;i<thlist.size();i++){
                for (int j=0;j<fh.size();j++){
                    //如果发货单已经存在退货单中，并且退货单的审核状态不为否决
                    if(thlist.get(i).getFahuo().getFhId() == fh.get(j).getFhId() && !thlist.get(i).getThSh().equals("否决")){
                        fhok.add(fh.get(j));
                        break;
                    }
                }
            }
            for(int a=fh.size()-1;a>=0;a--){
                for(int b=fhok.size()-1;b>=0;b--){
                    if(fh.get(a)==fhok.get(b)){
                        fh.remove(a);
                        break;
                    }
                }
            }
            fhok.clear();

        }
        //查看所有的换货单
        List<Huanhuo> hhlist = tuiHuoService.selectAllHuanHuo(null,null,null,null);
        System.err.println("hhlistSize:"+hhlist.size());
        if(thlist.size() != 0){
            for(int i=0;i<hhlist.size();i++){
                for (int j=0;j<fh.size();j++){
                    //如果发货单已经存在换货单中，并且换货单的审核状态不为否决
                    if(hhlist.get(i).getFahuo().getFhId() == fh.get(j).getFhId() && !hhlist.get(i).getHhSh().equals("否决")){
                        fhok.add(fh.get(j));
                        break;
                    }
                }
            }
            for(int a=fh.size()-1;a>=0;a--){
                for(int b=fhok.size()-1;b>=0;b--){
                    if(fh.get(a)==fhok.get(b)){
                        fh.remove(a);
                        break;
                    }
                }
            }
            fhok.clear();
        }
        return fh;
    }

    //换货}据ddId查到此订单的所有发货状态为‘发货成功’的发货单
    @RequestMapping("/selectFaHuoByddIdHHfhcg")
    @ResponseBody
    public List<Fahuo> selectFaHuoByddIdHHfhcg(Integer ddId){
        List<Fahuo> fh = faHuoService.selectFaHuoByddIdTHfhcg(ddId);
        return fh;
    }

    //退货 ==> 根据发货id查看发货详情
    @RequestMapping("/selectFaHuoXiangQinTuiHuo")
    @ResponseBody
    public List<Fahuoxiangqing> selectFaHuoXiangQinTuiHuo(Integer fhId){
        List<Fahuoxiangqing> fh = faHuoService.selectFaHuoXiangQin(fhId);
       /* //根据发货id查看退货详情
        List<Tuihuoxiangqing> th = tuiHuoService.selectTHXQbyFhId(fhId);
        //根据发货id查看换货详情
        List<Huanhuoxiangqing> hh = tuiHuoService.selectHHXQbyFhId(fhId);*/
        return fh;
    }

    //换货 ==> 根据发货id查看发货详情
    @RequestMapping("/selectFaHuoXiangQinHuanHuo")
    @ResponseBody
    public List<Fahuoxiangqing> selectFaHuoXiangQinHuanHuo(Integer fhId){
        return faHuoService.selectFaHuoXiangQin(fhId);
    }


    @Autowired
    ChRuKuService chRuKuService;
    //退货入库
    @RequestMapping("/tuiHuoRuKu")
    @ResponseBody
    public void tuiHuoRuKu(Integer thId, String empName){
        chRuKuService.tuiHuoRuKu(thId,empName);
    }

    //换货入库
    @RequestMapping("/huanHuoRuKu")
    @ResponseBody
    public void huanHuoRuKu(Integer hhId, String empName){
        chRuKuService.huanHuoRuKu(hhId,empName);
    }

}
